package com.kern.academic.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 *  Redis 相关操作工具类
 * Created by caoshi at 21:28 2021-06-26
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 设置值
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置值, 并
     * @param key
     * @param value
     * @param time
     * @param unit
     * @return
     */
    public Boolean setEx(String key, String value, long time, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value);
        return redisTemplate.expire(key, time, unit);
    }

    /**
     * 取值
     * @param key
     * @return
     */
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     *  设置过期时间
     * @param key
     * @param time  时长
     * @param unit
     * @return
     */
    public Boolean expire(String key, long time, TimeUnit unit) {
        return redisTemplate.expire(key, time, unit);
    }

    /**
     * 设置过期时间
     * @param key
     * @param d    过期的那个时刻
     * @return
     */
    public Boolean expireAt(String key, Date d) {
        return redisTemplate.expireAt(key, d);
    }


    /**
     * 检查key 是否存在
     * @param key
     * @return
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }



}
